package com.bgs.engineering.api.sysUser.controller;

import com.bgs.engineering.api.sysUser.service.SysUserService;
import com.bgs.engineering.util.CodeUtils;
import com.bgs.engineering.util.RedisUtil;
import com.bgs.engineering.util.pageUtil.PageRequest;
import com.wf.captcha.base.Captcha;
import com.bgs.engineering.annotation.OptLog;
import com.bgs.engineering.api.sysUser.model.SysUserModel;
import com.bgs.engineering.common.controller.BaseController;
import com.bgs.engineering.common.errorCode.CommonException;
import com.bgs.engineering.common.response.CommonConstants;
import com.bgs.engineering.common.response.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
 * @ClassName SysUserController
 * @Description 用户管理 用于系统的登录  /login 默认登录地址 logout 登出地址
 * @Author zcy
 * @Date 2023/3/28 14:17
 **/
@RestController
@Api(tags = "用户管理")
@RequestMapping("/sysUser")
@Slf4j
public class SysUserController extends BaseController {

    @Autowired
    SysUserService sysUserService;
    /**
     * 保存用户
     * @param sysUserModel
     * @return
     */
    @PostMapping("/saveUser")
    @ApiOperation(value = "用户管理的新增方法")
    @OptLog(modular = "用户管理", type = CommonConstants.ADD, describe = "新增用户")
    public CommonResult saveUser(@RequestBody SysUserModel sysUserModel) throws CommonException {
        log.info("新增用户");
        return sysUserService.saveUser(sysUserModel);
    }
    /**
     * 修改用户信息
     * @param sysUserModel
     * @return
     */
    @PutMapping("/updateUser")
    @ApiOperation(value = "用户管理的修改方法")
    @OptLog(modular = "用户管理", type = CommonConstants.UPDATE, describe = "修改用户")
    public CommonResult updateUser(@RequestBody SysUserModel sysUserModel) throws CommonException {
        log.info("修改用户");
        return sysUserService.updateUser(sysUserModel);
    }
    /**
     * 删除用户信息
     * @param id
     * @return
     */
    @DeleteMapping("/delUserById")
    @ApiOperation(value = "用户管理的删除方法")
    @OptLog(modular = "用户管理", type = CommonConstants.DELETE, describe = "删除用户")
    public CommonResult delUserById(Integer id) throws CommonException {
        log.info("删除用户信息");
        return sysUserService.delUserById(id);
    }
    /**
     * 查询用户信息
     * @param sysUserModel
     * @return
     */
    @GetMapping("/getUserByPage")
    @ApiOperation(value = "用户管理的分页查询方法")
    @OptLog(modular = "用户管理", type = CommonConstants.SELECT, describe = "分页查询")
    public CommonResult getUserByPage(SysUserModel sysUserModel, PageRequest pageRequest) throws CommonException {
        log.info("分页查询");
        return sysUserService.getUserByPage(sysUserModel,pageRequest);
    }
    /**
     * @Author songyongcheng
     * 获取验证麻(返回的img属性可以直接放到图片得src中)登陆的时候需要携带code到(前端取data中的imgCode)
     * @Date 2023/3/30 14:10
     * @Return
     */
    @GetMapping("/code")
    @ApiOperation(value = "获取验证码")
    public CommonResult getCode(){
        // 调用验证码生成工具类
        Captcha captcha = CodeUtils.switchCaptcha();
        String captchaValue = captcha.text();
        String key = CommonConstants.CACHE_USER_PASSWORD_CODE+captchaValue;
        // 保存Redis中过期时间120秒
        RedisUtil.set(key,1,120);
        // 验证码信息
        return CommonResult.ok(captcha.toBase64());
    }
}
